package com.yzy.interceptor;

import com.yzy.util.ResponseCode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @description 异常拦截
 * @author mgl
 * @date 2023/7/9 09:22
 * @version 1.0
 */
@ControllerAdvice
public class ExceptionHandle {

    private final static Logger LOGGER = LoggerFactory.getLogger(ExceptionHandle.class);

    /**
     * @description 判断错误是否是已定义的已知错误，不是则由未知错误代替，同时记录在log中
     * @param e 异常
     * @return com.yzy.util.ResponseCode
     * @author mgl
     * @date 2023/7/9 09:11
     */
    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public ResponseCode exceptionGet(Exception e){
        if(e instanceof RuntimeException){
            RuntimeException myException = (RuntimeException) e;
            String message = myException.getMessage();

            LOGGER.error(message);

            if (message.equals("token_miss") ||
                message.equals("token_invalidate") ||
                message.equals("token_expire") ||
                message.equals("user_invalidate"))

                return ResponseCode.buildFailed(500,myException.getMessage());
        }

        LOGGER.error("【系统异常】{}",e);
        return null;
    }
}
